package com.sfdk.io
{
import com.sfdk.events.IOEvent;
import com.sfdk.log.Logger;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
    import flash.media.Sound;
    import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.utils.ByteArray;

public class GenericIO extends EventDispatcher
	{
		public static const XML_FILE : int = 0;
		public static const BITMAP_FILE : int = 1;
		public static const DATA_FILE : int = 2;
		public static const SWF_FILE : int = 1;
        public static const SOUND_FILE : int = 3;
		
		protected var _fileInfo : XML;
		protected var _imageData : BitmapData;
		protected var _fileData : ByteArray;
        protected var _soundData : Sound;
		protected var _fileLocation : String;
		
		public function GenericIO(fileLocation : String, type : int = XML_FILE)
		{
            // Strangely enough null + null = "0"
			if(fileLocation != null && fileLocation != "0")
			{
				_fileLocation = fileLocation;
				read(type);
			}
		}
		
		public function read(inputType : int = XML_FILE) : void
		{
			var urlRequest : URLRequest = new URLRequest(_fileLocation);
			var requestHeader:URLRequestHeader = new URLRequestHeader("Cache-Control","no-cache");
			urlRequest.requestHeaders=new Array(requestHeader);
			switch(inputType)
			{
				case XML_FILE : 
					var loader : URLLoader = new URLLoader(urlRequest);
					loader.addEventListener(Event.COMPLETE, loadFileAsXML);
					loader.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
					loader.load(urlRequest);
				break;
			
				case BITMAP_FILE :
					var bitmapLoader : Loader = new Loader();
					bitmapLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadFileAsBitmap);
					bitmapLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
					bitmapLoader.load(urlRequest);
				break;
				
				case DATA_FILE :
					var dataLoader : URLLoader = new URLLoader(urlRequest);
					dataLoader.dataFormat = URLLoaderDataFormat.BINARY;
					dataLoader.addEventListener(Event.COMPLETE, loadFileAsData);
					dataLoader.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
					dataLoader.load(urlRequest);
				break;

                case SOUND_FILE :
					var soundLoader : Sound = new Sound();
					soundLoader.addEventListener(Event.COMPLETE, loadFileAsSound);
					soundLoader.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
					soundLoader.load(urlRequest);
				break;
				
				default : 
					Logger.error("GenericIO :: Unknown type");
				break;
			}
		}
		
		/** XML File handling **/
		
		private function loadFileAsXML(event : Event) : void
		{
			_fileInfo = new XML(event.target.data);
			init();
		}		
		
		public function getFileData() : XML
		{
			return _fileInfo;
		}
		
		/** Bitmap File handling **/
		
		private function loadFileAsBitmap(event : Event) : void
		{
			var loader : Loader = Loader(event.target.loader);
			var image : Bitmap = Bitmap(loader.content);
			_imageData = image.bitmapData;
			init();
			dispatchEvent(new IOEvent(IOEvent.LOAD_COMPLETE));
		}		
		
		public function getImageData() : BitmapData
		{
			return _imageData;
		}
		
		/** Data File handling **/
		
		private function loadFileAsData(event : Event) : void
		{
            _fileData = ByteArray(event.target.data);
            init();
			dispatchEvent(new IOEvent(IOEvent.LOAD_COMPLETE));
		}
		
		public function getByteData() : ByteArray
		{
			return _fileData;
		}

        private function loadFileAsSound(event : Event) : void
        {
            _soundData = Sound(event.target);
            init();
            dispatchEvent(new IOEvent(IOEvent.LOAD_COMPLETE));
        }

        public function getSoundData() : Sound
        {
            return _soundData;
        }
		
		protected function init() : void
		{
			// to be overriden
		}
		
		/** Event Handlers **/
		
		private function onIoError(e : IOErrorEvent) : void
		{
			Logger.error("GenericIO :: "+e.text);
		}
	}
}